# plugs/op.py
#
#

""" op on join or op on !op command"""

__copyright__ = 'this file is in the public domain'

from gozerbot.generic import getwho, rlog
from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.callbacks import callbacks
from gozerbot.users import users
from gozerbot.plughelp import plughelp

plughelp.add('ops', 'op on command or op on join .. for op on join the user\
 needs to have the channel name in his/hers status .. see addstatus ')

splitted = []

def checksplit(bot, ievent):
    """ check if quit is because of a split """
    if '*.' in ievent.txt or bot.server in ievent.txt:
        splitted.append((bot.name, ievent.nick.lower()))

callbacks.add('QUIT', checksplit, None)

def opjoincb(bot, ievent):
    """ see if we should op a user on join """
    # don't try to op the bot
    if ievent.nick == bot.nick:
        return
    if (bot.name, ievent.nick.lower()) not in splitted:
        if users.status(ievent.userhost, ievent.channel.upper()):
            if ievent.channel not in bot.opchan:
                return
            bot.doop(ievent.channel, ievent.nick)
    else:
        try:
            splitted.remove((bot.name, ievent.nick.lower()))
        except:
            pass

callbacks.add('JOIN', opjoincb, None)

def handle_op1(bot, ievent):
    """ op [<nick>] .. op an user """
    if ievent.channel not in bot.opchan:
        ievent.reply("i'm not op in %s" % ievent.channel)
        return
    try:
        nick = ievent.args[0]
    except IndexError:
        nick = ievent.nick
    userhost = getwho(bot, nick)
    if not userhost:
        userhost = ievent.userhost
    if users.status(userhost, ievent.channel.upper()):
        bot.doop(ievent.channel, nick)
    else:
        ievent.reply("%s doesn't have %s status" % (nick, \
ievent.channel.upper()))

cmnds.add('op', handle_op1, 'USER')
examples.add('op', 'op [<nick>] .. give ops to <nick> or op the person \
giving the command', '1) op 2) op dunker')

def handle_splitted(bot, ievent):
    """ splitted .. show splitted list """
    ievent.reply(str(splitted))

cmnds.add('splitted', handle_splitted, 'OPER')
examples.add('splitted', 'show whos on the splitted list', 'splitted')

def handle_splittedclear(bot, ievent):
    """ splitted-clear .. clear splitted list """
    splitted = []
    ievent.reply('done')

cmnds.add('splitted-clear', handle_splittedclear, 'OPER')
examples.add('splitted-clear', 'clear the splitted list', 'splitted-clear')

def checkmode(bot, ievent):
    """ check mode string """
    plus = 0
    teller = 0
    try:
        splitted = ievent.postfix.split()
        chan = splitted[0].lower()
        modestr = splitted[1]
        who = splitted[2:]
    except:
        rlog(10, 'op', 'unknow mode string format: %s' % str(ievent))
        return
    for i in modestr:
        if i == '+':
            plus = 1
            continue
        if i == '-':
            plus = 0
            continue
        if i == 'o' and plus:
            if who[teller].lower() == bot.nick.lower():
                rlog(1, 'irc', 'opped on %s' % chan)
                bot.opchan.append(chan)
        if i == 'o' and not plus:
            if who[teller].lower() == bot.nick.lower():
                rlog(1, 'irc', 'deopped on %s' % chan)
                try:
                    bot.opchan.remove(chan)
                except:
                    pass
        teller += 1

callbacks.add('MODE', checkmode, None)
